Imports DAL
Imports MyClasses
Imports System.Text
Public Class BTax

    Public Shared Function Search(ByVal Taxid As Integer) As DataSet
        'Saves New Tax Entry in Mtax
        Return DTax.Search(Taxid)
    End Function
    Public Shared Function Save(ByVal Tax As CTax, ByVal SaveUpdate As String) As Boolean
        'Saves New Tax Entry in Mtax
        Return DTax.Save(Tax, SaveUpdate)
    End Function
    Public Shared Function GetTax(ByVal ItemID As Integer) As DataTable
        Return DTax.GetTax(ItemID)
    End Function
    '_20080304 : Add Overloaded function for TAX for SALE on PI
    Public Shared Function GetTax(ByVal ItemID As Integer, ByVal CLRcode As String) As DataTable
        Return DTax.GetTax(ItemID, CLRcode)
    End Function
    Public Shared Function GetTaxDeatils() As DataTable
        Return DTax.GetTaxDeatils()
    End Function
    Public Shared Function GetApplyTo(ByVal TaxName As String) As DataTable
        Return DTax.GetApplyTo(TaxName)
    End Function

    Public Shared Function GetTaxID(ByVal TaxName As String) As Integer
        Return DTax.GetTaxID(TaxName)
    End Function

    Public Shared Function GetDefaultTaxID() As DataTable
        Return DTax.GetDefaultTaxID()
    End Function

    Public Shared Function GetItemTaxes(ByVal ItemID As Integer, ByVal CLRcode As String) As TaxPI()
        Dim dtbTax As New DataTable
        Try
            dtbTax = BTax.GetTax(ItemID, CLRcode)
            If dtbTax.Rows.Count > 0 Then
                Return GetItemTaxes(dtbTax)
            End If
            Return Nothing
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "BAL", "BTAX", "GetItemTaxes")
            Return Nothing
        End Try
    End Function

    Public Shared Function GetItemTaxes(ByVal dtbTax As DataTable) As TaxPI()
        Try
            Dim intCount As Integer
            If dtbTax.Rows.Count > 0 Then
                Dim ItemTaxes(dtbTax.Rows.Count - 1) As TaxPI
                For intCount = 0 To dtbTax.Rows.Count - 1
                    ItemTaxes(intCount) = New TaxPI
                    ItemTaxes(intCount).TaxID = dtbTax.Rows(intCount).Item(0)           'TAX ID
                    ItemTaxes(intCount).TaxName = dtbTax.Rows(intCount).Item(1)         'TAX Name
                    ItemTaxes(intCount).TaxValue = dtbTax.Rows(intCount).Item(2)        'RATE
                    ItemTaxes(intCount).TaxAmount = dtbTax.Rows(intCount).Item(3)      'AMOUNT
                    ItemTaxes(intCount).ApplyTo = dtbTax.Rows(intCount).Item(4)      'ApplyTo
                Next
                Return ItemTaxes
            End If
            Return Nothing
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "BAL", "BTAX", "GetItemTaxes1")
            Return Nothing
        End Try

    End Function

    Public Shared Function GetTotalTaxAmtString(ByVal ItemTaxes As TaxPI()) As String
        Try
            If IsNothing(ItemTaxes) OrElse ItemTaxes.Length <= 0 Then Return String.Empty
            Dim strTaxes As New StringBuilder
            For Each Tax As TaxPI In ItemTaxes
                strTaxes.Append(Tax.TaxName & "=" & Tax.TaxAmount & ",")
            Next
            If strTaxes.Length > 0 Then
                Return strTaxes.Replace(",", "", strTaxes.Length - 1, 1).ToString()
            End If
            Return String.Empty
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, "BAL", "BTAX", "GetTotalTaxAmtString")
            Return Nothing
        End Try
    End Function

    Public Shared Function IsTaxUsed(ByVal TaxID As Integer) As Integer
        Return DTax.IsTaxUsed(TaxID)
    End Function

    Public Shared Function DeleteTax(ByVal TaxID As Integer) As Integer
        Return DTax.DeleteTax(TaxID)
    End Function

    Public Shared Function GetSaleTaxValue(ByVal ItemID As Long, ByVal CLRcode As String) As Double
        Return DTax.GetSaleTaxValue(ItemID, CLRcode)
    End Function

End Class
